const roleService = require("../services/roleService");

class RoleController {
  // 创建角色
  async create (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await roleService.create(data, user_id);

    ctx.body = {
      code: 200,
      data: null,
      message: '创建成功'
    }
  }

  // 更新角色
  async update (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await roleService.update(data, user_id);
    ctx.body = {
      code: 200,
      data: null,
      message: '更新成功'
    }
  }

  // 删除角色
  async delete (ctx) {
    const { id } = ctx.query;
    await roleService.delete(id);
    ctx.body = {
      code: 200,
      data: null,
      message: '删除成功'
    }
  }

  // 获取单个角色信息
  async findOne (ctx) {
    const { id } = ctx.query;
    const role = await roleService.findOne(id);
    ctx.body = {
      code: 200,
      data: role,
      message: '查询成功'
    }
  }

  // 获取所有角色
  async findAll (ctx) {
    const roles = await roleService.findAll();
    ctx.body = {
      code: 200,
      data: roles,
      message: 'Success'
    }
  }
}

module.exports = new RoleController();